using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BlueFinity.mvNET.CoreObjects;
using mvLibrary;
using mvLibrary.Controls;
using IBMU2.UODOTNET;



namespace mvLibrary
{
    public partial class CalendarForm : BaseForm
    {
        public CalendarForm()
        {
            InitializeComponent();

        }
        public void ConnectPick(string operation)
        {
            string ItemAsString = UniItem.ToString();
            UniSubroutine subRoutine = programCaller.subRoutineManager.CreateSubRoutine(UniAccount, "CALENDAR.NET", operation, ItemAsString, _itemId);

            ItemAsString = subRoutine.GetArg(1);
        }
        private void CheckAllRows(bool checkRows, int attributeNumber)
        {
            string checkString;
            int columnNumber;
            if (attributeNumber == 2)
            {
                checkString = "X";
                columnNumber = 1;
            }
            else
            {
                checkString = "S";
                columnNumber = 2;
            }
            int a;
            if (!checkRows)
            {
                checkString = "";
            }
            for (a = 0; a < grillaDiaria.Rows.Count; ++a)
            {
                grillaDiaria.Rows[a].Cells[columnNumber].Value = (checkRows == true) ? true : false;

                UniItem.Replace(attributeNumber, a + 1, checkString);
            }
        }
        private void SaveDay()
        {
            ConnectPick("W");
            UniOldItem = UniItem;
        }
        private void ReadDay()
        {
            UniItem.Dispose();
            ConnectPick("R");

            UniOldItem = UniItem;

            base.FillControls(panelData);
        }
        private void TransferDay()
        {
            ConnectPick("T");
            ShowDay();
            CheckAllRows(false, 5);
        }
        public void ConvertDateToPickFormat(string DateSelected)
        {
            CancelItem();
            textItemId.Text = User + "*" + DataBASIC.IConv(DateSelected, "D");
            SetItemId();
            ReadDay();
            panelData.Enabled = true;

        }
        private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
        {
            ConvertDateToPickFormat(e.Start.ToShortDateString());
            label1.Text = e.Start.ToLongDateString();
        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            SaveDay();
            // ConvertDateToPickFormat(e.Start.ToShortDateString());
        }
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            FillControls(panelData);
            panelData.Enabled = true;
            ItemId = textItemId.Text; // Se reasigna pq BaseForm.Load()lo limpia
        }

        private void CalendarForm_Load(object sender, EventArgs e)
        {
            ToolBoxVisible(false);
            _itemId = User + "*" + DataBASIC.IConv(DateTime.Now.ToShortDateString(), "D2/");
            ConnectPick("S");
            DateTime StartDate = Convert.ToDateTime(DataBASIC.OConv(DataBASIC.Field(ItemId, "*", 2), "D2/"));
            monthCalendar1.SelectionStart = StartDate;
            monthCalendar1.SelectionEnd = StartDate;
            textItemId.Text = ItemId;
            label1.Text = monthCalendar1.SelectionStart.ToLongDateString();
        }

        private void butSave_Click(object sender, EventArgs e)
        {
            SaveDay();
        }

        private void butTransfer_Click(object sender, EventArgs e)
        {
            if (textTransfer.Text == "")
            {
                MessageBox.Show("Ingrese una fecha");
                return;
            }
            TransferDay();
        }
        private void ShowDay()
        {
            grillaDiaria.Rows.Clear();
            grillaMensual.Rows.Clear();
            grillaAnual.Rows.Clear();
            base.FillControls(panelData);
            DateTime StartDate = Convert.ToDateTime(DataBASIC.OConv(DataBASIC.Field(ItemId, "*", 2), "D2/"));
            monthCalendar1.SelectionStart = StartDate;
            monthCalendar1.SelectionEnd = StartDate;
            textItemId.Text = ItemId;
            textTransfer.Text = "";
            checkTransfer.Checked = false;
            checkCancel.Checked = false;
            label1.Text = monthCalendar1.SelectionStart.ToLongDateString();
        }
        private void checkCancel_CheckStateChanged(object sender, EventArgs e)
        {
            CheckAllRows(checkCancel.Checked, 2);
            SaveDay();
        }

        private void checkTransfer_CheckStateChanged(object sender, EventArgs e)
        {
            CheckAllRows(checkTransfer.Checked, 5);
        }

        private void butVencimientos_Click(object sender, EventArgs e)
        {
            if (textVencimientos.Text == "")
            {
                MessageBox.Show("Ingrese una fecha");
                textVencimientos.Focus();
                return;
            }
            int PickDate = int.Parse(DataBASIC.IConv(textVencimientos.Text, "D"));
            int[] Vencimientos = new int[6];
            Vencimientos[0] = PickDate + 30;
            Vencimientos[1] = PickDate + 45;
            Vencimientos[2] = PickDate + 60;
            Vencimientos[3] = PickDate + 90;
            Vencimientos[4] = PickDate + 120;
            Vencimientos[5] = PickDate + 180;
            label2.Text = DataBASIC.OConv(Vencimientos[0].ToString(), "D2/");
            label3.Text = DataBASIC.OConv(Vencimientos[1].ToString(), "D2/");
            label4.Text = DataBASIC.OConv(Vencimientos[2].ToString(), "D2/");
            label5.Text = DataBASIC.OConv(Vencimientos[3].ToString(), "D2/");
            label6.Text = DataBASIC.OConv(Vencimientos[4].ToString(), "D2/");
            label7.Text = DataBASIC.OConv(Vencimientos[5].ToString(), "D2/");
        }

        private void butSearch_Click(object sender, EventArgs e)
        {
            CalendarSearchForm FormBuscar = new CalendarSearchForm();
            FormBuscar.User = User;
            FormBuscar.UniAccount = UniAccount;
            FormBuscar.ShowDialog();
            if (DataBASIC.Field(CalendarSearchForm.itemid, "*", 2) != "")
            {
                _itemId = CalendarSearchForm.itemid;
                ShowDay();
            }
        }

        private void checkTransfer_CheckedChanged(object sender, EventArgs e)
        {
            if (checkTransfer.Checked)
            {
                textTransfer.Focus();
            }
        }

        private void checkCancel_CheckedChanged(object sender, EventArgs e)
        {
            butSave.Focus();
        }

        private void butSelect_Click(object sender, EventArgs e)
        {
            if (textSelect.Text == "")
            {
                MessageBox.Show("Ingrese una fecha");
                textSelect.Focus();
                return;
            }
            DateTime StartDate = Convert.ToDateTime(textSelect.Text);
            monthCalendar1.SelectionStart = StartDate;
            monthCalendar1.SelectionEnd = StartDate;
            ConvertDateToPickFormat(textSelect.Text);
            label1.Text = monthCalendar1.SelectionStart.ToLongDateString();

        }

        private void butPDF_Click(object sender, EventArgs e)
        {
            ExportItem(ReportFileFormat.PDF);
        }

        private void butXLS_Click(object sender, EventArgs e)
        {
            ExportItem(ReportFileFormat.XLS);
        }

        private void CalendarForm_KeyPress(object sender, KeyPressEventArgs e)
        {

        }
        // actualizar el  calendario cuando se usan las arrow keys
        private void ChangeCalendarDate(int increment)
        {
            int internalDate = Convert.ToInt32(DataBASIC.Field(ItemId, "*", 2)) + increment;
            DateTime date = Convert.ToDateTime(DataBASIC.OConv(internalDate.ToString(), "D2/"));
            ConvertDateToPickFormat(date.ToShortDateString());
            label1.Text = date.ToLongDateString();
            monthCalendar1.SelectionStart = date;
            monthCalendar1.SelectionEnd = date;
        }
        private void CalendarForm_KeyUp(object sender, KeyEventArgs e)
        {
            // traslado ?
            if (e.KeyCode == Keys.T)
            {
                if (grillaAnual.IsCurrentCellInEditMode | grillaDiaria.IsCurrentCellInEditMode | grillaMensual.IsCurrentCellInEditMode)
                {
                    return;
                }
                string rowsList = Microsoft.VisualBasic.Interaction.InputBox("Notas que traslada (entre comas) o cero trasladar todas", "Trasladar notas", "", -1, -1);
                if (rowsList == "") return;

                string tDate = Microsoft.VisualBasic.Interaction.InputBox("Fecha a la que traslada", "Trasladar notas", "", -1, -1);
                if (tDate == "") return;
                if (DataBASIC.Num(DataBASIC.IConv(tDate, "D2/")))
                {
                    // trasladar todos
                    if (rowsList == "0")
                    {
                        checkTransfer.Checked = true;
                        CheckAllRows(checkTransfer.Checked, 5);
                    }
                    else
                    {
                        int rowsCount = UniItem.Dcount(5);
                        string[] rowsToMove = rowsList.Split(',');
                        foreach (string row in rowsToMove)
                            try
                            {
                                int rowNumber = Convert.ToInt16(row);
                                if (rowNumber <= rowsCount)
                                {
                                    grillaDiaria.Rows[rowNumber - 1].Cells[2].Value = true;
                                    UniItem.Replace(5, rowNumber, "S");
                                }
                            }
                            catch
                            {
                                MessageBox.Show("Indique renglones separados por comas. Ej: 1,4,5", "Error al trasladar", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return;
                            }
                    }
                    UniItem.Replace(10, DataBASIC.IConv(tDate, "D2/"));
                    textTransfer.Text = tDate;
                    TransferDay();
                }
                return;
            }

            // cambiar la fecha cuando se presionan las arrow keys
            if (e.KeyCode == Keys.Left)
            {
                ChangeCalendarDate(-1);
            }
            if (e.KeyCode == Keys.Right)
            {
                ChangeCalendarDate(1);
            }
            if (e.KeyCode == Keys.Up)
            {
                ChangeCalendarDate(-7);
            }
            if (e.KeyCode == Keys.Down)
            {
                ChangeCalendarDate(7);
            }
        }
    }
}
